<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="批次名称" prop="patchName">
        <el-input
          v-model="queryParams.patchName"
          placeholder="请输入批次名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的种苗繁育记录ID" prop="patchZhongMiaoID">
        <el-input
          v-model="queryParams.patchZhongMiaoID"
          placeholder="请输入关联的种苗繁育记录ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的栽培记录ID" prop="patchZaiPeiID">
        <el-input
          v-model="queryParams.patchZaiPeiID"
          placeholder="请输入关联的栽培记录ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的采收记录ID" prop="patchCaiShouID">
        <el-input
          v-model="queryParams.patchCaiShouID"
          placeholder="请输入关联的采收记录ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的加工记录ID" prop="patchJiaGongID">
        <el-input
          v-model="queryParams.patchJiaGongID"
          placeholder="请输入关联的加工记录ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联的质量检测记录ID" prop="patchZhiLiJianCeID">
        <el-input
          v-model="queryParams.patchZhiLiJianCeID"
          placeholder="请输入关联的质量检测记录ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['seedlingSys:patch:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['seedlingSys:patch:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['seedlingSys:patch:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['seedlingSys:patch:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="patchList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="批次唯一标识" align="center" prop="patchID" />
      <el-table-column label="批次名称" align="center" prop="patchName" />
      <el-table-column
        label="批次描述"
        align="center"
        prop="patchDescription"
      />
      <el-table-column
        label="批次图片"
        align="center"
        prop="patchImage"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.patchImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column
        label="关联的种苗繁育记录ID"
        align="center"
        prop="patchZhongMiaoID"
      />
      <el-table-column
        label="关联的栽培记录ID"
        align="center"
        prop="patchZaiPeiID"
      />
      <el-table-column
        label="关联的采收记录ID"
        align="center"
        prop="patchCaiShouID"
      />
      <el-table-column
        label="关联的加工记录ID"
        align="center"
        prop="patchJiaGongID"
      />
      <el-table-column
        label="关联的质量检测记录ID"
        align="center"
        prop="patchZhiLiJianCeID"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['seedlingSys:patch:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['seedlingSys:patch:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改批次管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <div class="dialog-header">
        <el-button type="text" icon="el-icon-refresh" @click="refreshOptions"
          >刷新选项</el-button
        >
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="批次名称" prop="patchName">
          <el-input v-model="form.patchName" placeholder="请输入批次名称" />
        </el-form-item>
        <el-form-item label="批次描述" prop="patchDescription">
          <el-input
            v-model="form.patchDescription"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="批次图片" prop="patchImage">
          <image-upload v-model="form.patchImage" />
        </el-form-item>
        <el-form-item label="关联种苗" prop="patchZhongMiaoID">
          <el-select
            v-model="form.patchZhongMiaoID"
            placeholder="请选择种苗记录"
          >
            <el-option
              v-for="item in zhongmiaoOptions"
              :key="item.zhongMiaoID"
              :label="item.zhongMiaoDisplayName"
              :value="item.zhongMiaoID"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="关联栽培" prop="patchZaiPeiID">
          <el-select v-model="form.patchZaiPeiID" placeholder="请选择栽培记录">
            <el-option
              v-for="item in zaipeiOptions"
              :key="item.zaiPeiID"
              :label="item.zaiPeiDisplayName"
              :value="item.zaiPeiID"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="关联采收" prop="patchCaiShouID">
          <el-select v-model="form.patchCaiShouID" placeholder="请选择采收记录">
            <el-option
              v-for="item in caishouOptions"
              :key="item.caiShouID"
              :label="item.caiShouDisplayName"
              :value="item.caiShouID"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="关联加工" prop="patchJiaGongID">
          <el-select v-model="form.patchJiaGongID" placeholder="请选择加工记录">
            <el-option
              v-for="item in jiagongOptions"
              :key="item.jiaGongID"
              :label="item.jiaGongDisplayName"
              :value="item.jiaGongID"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="关联质检" prop="patchZhiLiJianCeID">
          <el-select
            v-model="form.patchZhiLiJianCeID"
            placeholder="请选择质检记录"
          >
            <el-option
              v-for="item in zhilijianceOptions"
              :key="item.zhiLiJianCeID"
              :label="item.zhiLiJianCeTestName"
              :value="item.zhiLiJianCeID"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPatch,
  getPatch,
  delPatch,
  addPatch,
  updatePatch,
} from "@/api/seedlingSys/patch";
import { listZhongmiao } from "@/api/seedlingSys/zhongmiao";
import { listZaipei } from "@/api/seedlingSys/zaipei";
import { listCaishou } from "@/api/seedlingSys/caishou";
import { listJiagong } from "@/api/seedlingSys/jiagong";
import { listZhilijiance } from "@/api/seedlingSys/zhilijiance";

export default {
  name: "Patch",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 批次管理表格数据
      patchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 各环节的选项数据
      zhongmiaoOptions: [],
      zaipeiOptions: [],
      caishouOptions: [],
      jiagongOptions: [],
      zhilijianceOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patchName: null,
        patchZhongMiaoID: null,
        patchZaiPeiID: null,
        patchCaiShouID: null,
        patchJiaGongID: null,
        patchZhiLiJianCeID: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        patchName: [
          { required: true, message: "批次名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getZhongmiaoOptions();
    this.getZaipeiOptions();
    this.getCaishouOptions();
    this.getJiagongOptions();
    this.getZhilijianceOptions();
  },
  methods: {
    /** 查询批次管理列表 */
    getList() {
      this.loading = true;
      listPatch(this.queryParams).then((response) => {
        this.patchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patchID: null,
        patchName: null,
        patchDescription: null,
        patchImage: null,
        patchZhongMiaoID: null,
        patchZaiPeiID: null,
        patchCaiShouID: null,
        patchJiaGongID: null,
        patchZhiLiJianCeID: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.patchID);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.refreshOptions();
      this.open = true;
      this.title = "添加批次管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const patchID = row.patchID || this.ids;
      this.refreshOptions();
      getPatch(patchID).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改批次管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.patchID != null) {
            updatePatch(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPatch(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const patchIDs = row.patchID || this.ids;
      this.$modal
        .confirm('是否确认删除批次管理编号为"' + patchIDs + '"的数据项？')
        .then(function () {
          return delPatch(patchIDs);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "seedlingSys/patch/export",
        {
          ...this.queryParams,
        },
        `patch_${new Date().getTime()}.xlsx`
      );
    },
    /** 刷新所有选项数据 */
    refreshOptions() {
      this.getZhongmiaoOptions();
      this.getZaipeiOptions();
      this.getCaishouOptions();
      this.getJiagongOptions();
      this.getZhilijianceOptions();
    },
    /** 获取种苗选项 */
    getZhongmiaoOptions() {
      listZhongmiao().then((response) => {
        this.zhongmiaoOptions = response.rows;
      });
    },
    /** 获取栽培选项 */
    getZaipeiOptions() {
      listZaipei().then((response) => {
        this.zaipeiOptions = response.rows;
      });
    },
    /** 获取采收选项 */
    getCaishouOptions() {
      listCaishou().then((response) => {
        this.caishouOptions = response.rows;
      });
    },
    /** 获取加工选项 */
    getJiagongOptions() {
      listJiagong().then((response) => {
        this.jiagongOptions = response.rows;
      });
    },
    /** 获取质检选项 */
    getZhilijianceOptions() {
      listZhilijiance().then((response) => {
        this.zhilijianceOptions = response.rows;
      });
    },
  },
};
</script>
